package com.gf.gf_data.sys.redis;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

/**
 * Redis 对象（反）序列化器
 *
 * Created by yxl on 2017/12/19
 */
public class RedisObjectSerializer implements RedisSerializer<Object> {

    /**
     * 序列化
     */
    private Converter<Object, byte[]> serializer = new SerializingConverter();
    /**
     * 反序列化
     */
    private Converter<byte[], Object> deserializer = new DeserializingConverter();

    private static final byte[] EMPTY_ARRAY = new byte[0];

    @Override
    public byte[] serialize(Object o) throws SerializationException {
        if (o == null) return EMPTY_ARRAY;
        try {
            return serializer.convert(o);
        } catch (Exception e) {
            return EMPTY_ARRAY;
        }
    }

    @Override
    public Object deserialize(byte[] bytes) throws SerializationException {
        if (bytes == null || bytes.length == 0) return null;
        try {
            return deserializer.convert(bytes);
        } catch (Exception e) {
            throw new SerializationException("Can not deserialize", e);
        }
    }

}
